﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IMCOM.Imagiality.Core;
using System.Drawing;
using System.Windows.Forms;
using IMCOM.Imagiality.ImageProcessing.Canvas2D.Commands;

namespace IMCOM.Imagiality.ImageProcessing.Canvas2D
{
    public class CanvasImageWrapper
    {
        private IList<GenericImage> originalImages;
        private double scalingFactor;
        private IList<CanvasImageWrapperObserver> observers;
        private int activeImageIndex;
        
        public CanvasImageWrapper(IList<GenericImage> images)
        {
            scalingFactor = 1.0;
            observers = new List<CanvasImageWrapperObserver>();
            originalImages = images;
        }

        public CanvasImageWrapper(GenericImage image)
        {
            originalImages = new List<GenericImage>();
            originalImages.Add(image);
            scalingFactor = 1.0;
            observers = new List<CanvasImageWrapperObserver>();
        }

        public IList<GenericImage> OriginalImages
        {
            get { return originalImages; }
            set { originalImages = value; activeImageIndex = value.Count - 1; NotifyObservers(); }
        }

        public double ScalingFactor
        {
            get { return scalingFactor; }
            set { scalingFactor = value; }
        }

        public int ActiveImageIndex
        {
            get { return activeImageIndex; }
            set { activeImageIndex = value; }
        }

        public GenericImage ActiveImage
        {
            get { return OriginalImages[activeImageIndex]; }
        }

        public IList<GenericImage> getProcessedImages(IList<GenericImage> input)
        {
            OriginalImages = input;
            return input;
        }

        public void ExecuteCommand(CanvasImageCommand canvasImageCommand)
        {
            canvasImageCommand.Execute(this);
            NotifyObservers();
        }

        public void AddObserver(CanvasImageWrapperObserver observer)
        {
            observers.Add(observer);
            NotifyObservers();
        }

        public GenericImage ViewImage
        {
            get
            {
                GenericImage activeGenericImage = OriginalImages[activeImageIndex];
                Bitmap activeBitmap = activeGenericImage.getBitmap();
                Bitmap newBitmap;

                newBitmap = new Bitmap((int)(activeBitmap.Width * ScalingFactor), (int)(activeBitmap.Height * ScalingFactor));

                Graphics g = Graphics.FromImage(newBitmap);

                g.DrawImage(activeBitmap, 0, 0, (int)(activeBitmap.Width * ScalingFactor), (int)(activeBitmap.Height * ScalingFactor));
                GenericImage newGenericImage = new GenericImage(newBitmap);
                newGenericImage.RegionOfInterest = new Rectangle((int)(activeGenericImage.RegionOfInterest.X * ScalingFactor),
                    (int)(activeGenericImage.RegionOfInterest.Y * ScalingFactor),
                    (int)(activeGenericImage.RegionOfInterest.Width * ScalingFactor),
                    (int)(activeGenericImage.RegionOfInterest.Height * ScalingFactor));

                return newGenericImage;
            }
          
        }

        protected void NotifyObservers()
        {
            if (activeImageIndex != -1)
            {
                foreach (CanvasImageWrapperObserver observer in observers)
                {
                    observer.Notify();
                }
            }
        }
    }
}
